﻿using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System;
using System.Data;
using System.Runtime;

using Kenly.DBFramework.TestModel.Purchasing;

namespace Kenly.DBFramework.Tests
{

    /// <summary>
    ///这是 ViewGateway 的测试类，旨在
    ///包含所有 ViewGateway(Convert) 单元测试
    ///</summary>
    [TestClass()]
    public class ViewGatewayConvertTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}

        //使用 TestInitialize 在运行每个测试前先运行代码
        [TestInitialize()]
        public void MyTestInitialize()
        {
        }

        //使用 TestCleanup 在运行完每个测试后运行代码
        [TestCleanup()]
        public void MyTestCleanup()
        {

        }

        #endregion



        /// <summary>
        /// 
        /// </summary>
        [TestMethod()]
        public void CreateInstancesTest()
        {
            string msg="ViewGateway.CreateInstances did not return expected value.";

            int count = 0;
            List<Vendor> vendors = Vendor.CreateInstances(count);

            Assert.IsTrue(vendors.Count == count, msg + "count=" + count);

            count = 2;
            vendors = Vendor.CreateInstances(count);

            Assert.IsTrue(vendors.Count == count, msg + "count=" + count);
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod()]
        public void ShadowCopyToTest()
        {
            string msg="ViewGateway.ShadowCopyTo did not return expected value.";

            Vendor vendor = new Vendor();
            vendor.VendorId = 1;

            Vendor targetVendor = null;

            vendor.ShadowCopyTo(targetVendor);
            Assert.IsTrue(targetVendor == null, msg + "targetVendor = null");

            targetVendor = new Vendor();
            vendor.ShadowCopyTo(targetVendor);

            Assert.IsTrue(vendor.VendorId == targetVendor.VendorId,
                "ViewGateway.ShadowCopyTo did not return expected value.");
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod()]
        public void ToTableTest()
        {
            string msg="ViewGateway.ToTable did not return expected value.";

            //DataTable dt = Vendor.ToTable(null);
            //Assert.IsTrue(dt.Rows.Count == 0,msg);

            List<Vendor> vendors = new List<Vendor>();
            DataTable dt = Vendor.ToTable(vendors);
            Assert.IsTrue(dt.Rows.Count == vendors.Count,msg);

            int count = 2;
            vendors = Vendor.CreateInstances(count);
            vendors[0].VendorId = 0;
            vendors[1].VendorId = 1;

            dt = Vendor.ToTable(vendors);
            Assert.IsTrue(dt.Rows.Count == vendors.Count,msg);
            Assert.IsTrue(dt.Columns.Contains("VendorId"), msg);
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod()]
        public void ToObjectsTest()
        {
            string msg = "ViewGateway.ToObjects did not return expected value.";

            int count = 2;
            List<Vendor>  vendors = Vendor.CreateInstances(count);
            vendors[0].VendorId = 0;
            vendors[1].VendorId = 1;

            DataTable dt = Vendor.ToTable(vendors);
            vendors = Vendor.ToObjects(dt);

            Assert.IsTrue(dt.Rows.Count == vendors.Count, msg);
            Assert.IsTrue(vendors[1].VendorId==1, msg);
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod()]
        public void ToColumnNameTest()
        {
            string msg = "ViewGateway.ToColumnName did not return expected value.";

            string propertyName = null;
            string columnName = Vendor.ToColumnName(propertyName);
            Assert.IsTrue(columnName == propertyName, msg + "propertyName = null");

            propertyName = "VendorId";
            columnName = Vendor.ToColumnName(propertyName);
            Assert.IsTrue(columnName == "VendorID", msg + "propertyName =" + propertyName);
        }


        /// <summary>
        /// 
        /// </summary>
        [TestMethod()]
        public void ToPropertyNameTest()
        {
            string msg = "ViewGateway.ToPropertyName did not return expected value.";

            string columnName = null;
            string propertyName = Vendor.ToPropertyName(columnName);
            Assert.IsTrue(columnName == propertyName, msg + "columnName = null");

            columnName = "VendorID";
            propertyName = Vendor.ToPropertyName(columnName);
            Assert.IsTrue(propertyName == "VendorId", msg + "columnName =" + columnName);
        }

      
    }
}
